package com.gbei.center.oauth.service.impl;

import com.gbei.center.oauth.mapper.PlatformUserMapper;
import com.gbei.center.oauth.model.PlatformUser;
import com.gbei.center.oauth.model.vo.UserInfo;
import com.gbei.center.oauth.service.LoginService;
import com.gbei.center.utils.MD5Encoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description: 注册/登陆相关接口实现
 * @author xiechengguang
 * @date 2019年1月8日
 */
@Service
public class LoginServiceImpl  implements LoginService {
	private Logger logger = LoggerFactory.getLogger(LoginServiceImpl.class);
	
	@Autowired
    private PlatformUserMapper platformUserMapper;

	/**
	 * 用户登录
	 * @param username
	 * @param password
	 * @return
	 */
	public Map<String,Object> checkLogin(String username, String password) {
		//返回信息
		Map<String,Object> result = new HashMap<>(2);
		logger.info(MessageFormat.format("用户登录 --> username:{0},password:{1}",username,password));
		PlatformUser correctUser = platformUserMapper.selectUserByNameAndPassword(username, new MD5Encoder("password", "MD5").encode(password));

		if (correctUser != null){
			result.put("result", true);
		}else {
			result.put("result", false);
		}
		result.put("user", correctUser);

		return result;
	}

	/**
	 * 根据用户ID查找记录
	 * @param userId
	 * @return
	 */
	public PlatformUser selectByUserId(String userId){
		return platformUserMapper.selectByUserId(userId);
	}

	/**
	 * 根据用户ID查找记录(返回到客户端)
	 * @param userId
	 * @return
	 */
	public UserInfo userInfoByUserId(String userId){
		return platformUserMapper.userInfoByUserId(userId);
	}





}
